package cn.edu.zafu.bigdata.sxa;

import java.io.*;


/**
 * 单词逆序线程
 *
 * @author 202211090121
 */
public class ReverseThread extends Thread {
    private final PrintWriter out;

    private final BufferedReader in;

    public ReverseThread(PrintWriter out, BufferedReader in) {
        this.out = out;
        this.in = in;
    }

    /**
     * 逆序线程运行方法
     */
    @Override
    public void run() {
        if (in == null || out == null) {
            return;
        }
        try {
            String input;
            while ((input = in.readLine()) != null) {
                out.println(reverseIt(input));
                out.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 逆序字符串
     *
     * @param source 字符串
     * @return 逆序后的字符串
     */
    private String reverseIt(String source) {
        int len = source.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = len - 1; i >= 0; i--) {
            sb.append(source.charAt(i));
        }
        return sb.toString();
    }
}